<?php
/**
 * 角色管理
 *
 * @author 孙晓晔
 * @version $Id: Role.class.php 1051 2010-04-07 08:55:38Z sunxy $
 */

require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Role extends InfoUtil {
	public $tbl = Constant::tbl_role;

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
	
		'1' => '创建时间',
		'2' => '更新时间',			
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}
	
	function layout($controller, $request) {
		$this->tab($controller, $request);
	}
	
	function tab($controller, $request) {
		$data = array(
			'角色信息' => array('cc'=>'info.php?do=Role.find', 'js'=>'1'),
			'角色权限' => array('cc'=>'info.php?do=RolePriv.find', 'js'=>'1'),
		
			'权限信息' => array('cc'=>'info.php?do=Priv.find', 'js'=>'1'),
		
			'功能定制' => array('cc'=>'info.php?do=Func.find', 'js'=>''),
			'定制日志' => array('cc'=>'info.php?do=Customize.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 * 查找
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/role/find');
		return true;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		
		$where = " WHERE 1=1";
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 * 列表
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['priv'] = $this->_priv($row['id']);
				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/role/list');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 * 获得权限信息
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _priv($role) {
		require_once(dirname(__FILE__) . '/RolePriv.class.php');
		$aa = new RolePriv();
		
		return $aa->_num($role);
	}
	
	function _num($role) {
		require_once(dirname(__FILE__) . '/../emp/EmpRole.class.php');
		$aa = new EmpRole();
		
		return $aa->_num($role);
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/role/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = $request['name'];
		$content= $request['content'];
		$status = $request['status'];
			
		if (empty($name)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return false;
		}

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, content, oper, status, createtime) VALUES('%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $name, $content, $_SESSION['id'], $status);
		$result = $db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 编辑
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/role/edit');
		return true;
	}

	/**
	 * 编辑修改
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
			
		$name = $request['name'];
		$content= $request['content'];
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET name = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $content, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/role/copy');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/role/view');
		return true;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		
		return $db->fetch_all($result);
	}
	
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$role = $this->id($this->tbl, $id);
	
		$controller->assign_by_ref('role', $role);
		
		require_once(dirname(__FILE__) . '/../emp/Emp.class.php');
		$aa = new Emp();
		
		$emp = $aa->_list();
		
		require_once(dirname(__FILE__) . '/../emp/EmpRole.class.php');
		$bb = new EmpRole();

		$data = array();
		$no = 1;
		while (list($k, $row) = each($emp)) {
			$row['no'] = $no++;
				
			if ($bb->_emp_role($row['id'], $role['id'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'info/role/emp');
		return true;
	}
	
	/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$role = $request['id'];
	
		require_once(dirname(__FILE__) . '/../emp/EmpRole.class.php');
		$aa = new EmpRole();
	
		$aa->_reset($code);
	
		$list = $request['list'];
		if (empty($list)) {
			return false;
		}
	
		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$aa->_update($emp, $role);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function priv($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$role = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('role', $role);
		
		require_once(dirname(__FILE__) . '/Priv.class.php');
		$aa = new Priv();
		
		$priv = $aa->_list();
		
		require_once(dirname(__FILE__) . '/RolePriv.class.php');
		$bb = new RolePriv();
		
		$data = array();
		$no = 1;
		while (list($k, $row) = each($priv)) {
			$row['no'] = $no++;
				
			if ($bb->_priv_role($id, $row['cc'], $row['action'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'info/role/priv');
		return true;
	}
	
	/**
	* 权限分配
	*
	* @param Controller $controller
	* @param array $request
	*/
	function privPost($controller, $request) {
		$role = $request['id'];
		$list = $request['list'];

		require_once(dirname(__FILE__) . '/RolePriv.class.php');
		$aa = new RolePriv();
		
		$aa->_reset($role);
		
		if (empty($list)) {
			return false;
		}

		$bb = explode(',', $list);
		while (list($k, $priv_id) = each($bb)) {
			$aa->_update($role, $priv_id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$insql = " in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		$query = $db->query($sql);

		$sql = "DELETE FROM " . Constant::tbl_role_priv . " WHERE role " . $insql;
		// debug($sql);
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}
?>